/**
 * 对话框组件
 * 统一提示文案和外观
 */
import { Modal } from 'antd';
import './index.less';

const iconTypes = {
  info: 'info-circle',
  success: 'check-circle',
  error: 'cross-circle',
  warning: 'exclamation-circle',
};
const getWidthByMsg = (msg: React.ReactNode | number) => {
  if (typeof msg === 'string') {
    if (msg.length <= 14) {
      return 300;
    }
  }
  return 420;
};
export default {
  tip(type?: any, msg?: React.ReactNode | string, yesFn?: () => void, opt = {}) {
    const icon = type && iconTypes[type];
    Modal.info({
      content: msg,
      onOk: yesFn,
      type,
      icon,
      okText: '关闭',
      width: opt.width | getWidthByMsg(msg),
      ...opt,
    });
  },

  confirm(msg?: React.ReactNode | string, yesFn?: () => void, noFn?: () => void, opt = {}) {
    Modal.confirm({
      content: msg,
      onOk: yesFn,
      onCancel: noFn,
      okText: '确定',
      cancelText: '取消',
      width: opt.width | getWidthByMsg(msg),
      ...opt,
    });
  },
  alert(msg?: React.ReactNode | string, yesFn?: () => void, opt?: {}) {
    this.tip(null, msg, yesFn, opt);
  },
  info(msg?: React.ReactNode | string, yesFn?: () => void, opt?: {}) {
    this.tip('info', msg, yesFn, opt);
  },
  success(msg?: React.ReactNode | string, yesFn?: () => void) {
    this.tip('success', msg, yesFn);
  },
  error(msg?: React.ReactNode | string, yesFn?: () => void, opt?: {}) {
    this.tip('error', msg, yesFn, opt);
  },
  warning(msg?: React.ReactNode | string, yesFn?: () => void, opt?: {}) {
    this.tip('warning', msg, yesFn, opt);
  },
};
